# _*_ coding: UTF-8 _*_
# @Time     : 2021/10/28 16:47
# @Author   : 郑鲁明
# @Site     : http://www.hzdledu.cn/
# @File     : c11_ATM.py
# @Software : PyCharm


# 2.用面向对象的方法实现一个ATM

class ATM:
    def __init__(self):
        self.__alluser = {
            '张三': {
                'password': '123456',
                'money': 1000
            },
            '郑鲁明': {
                'password': '321321',
                'money': 2000
            }
        }

    def login(self):  # 验证账号是否存在
        print('-----------欢迎来到只存不取银行-----------')
        self.__user = input('请输入您的账号：')
        if self.__user in self.__alluser:
            self.verify_pwd()
        else:
            print('您输入的账号有误')

    def verify_pwd(self):  # 验证密码
        for i in range(2, -1, -1):
            pass_word = input('请输入您的密码：')
            if self.__alluser[self.__user]['password'] == pass_word:
                self.main_menu()
            else:
                if i != -1:
                    print(f'您输入的密码有误,你还有{i}次机会')
                    continue
                else:
                    self.exit()

    def main_menu(self):  # 主菜单
        print('''
---------主菜单---------
-1. 查询余额
-2. 取款
-3. 存款
-4. 转账
-5. 退出
------------------------''')
        print(f'{self.__user},你好！')
        oper = input('请输入您要办的业务')
        if oper == '1':
            self.query_balance()
        elif oper == '2':
            self.check_out()
        elif oper == '3':
            self.save_money()
        elif oper == '4':
            self.transfer()
        elif oper == '5':
            self.exit()
        else:
            print('请输入“1-5”之间的数字')
            self.main_menu()

    def query_balance(self):  # 查询余额
        __user_money = self.__alluser[self.__user]['money']
        print(__user_money)
        self.main_menu()


    def check_out(self):  # 取钱
        __check_money = input('请输入您想要取走的数目：')
        if __check_money.isnumeric():
            if int(__check_money) > self.__alluser[self.__user]['money']:
                print('您的余额不够')
                self.check_out()
            else:
                self.__alluser[self.__user]['money'] -= int(__check_money)
                print(f"您现在所剩余额{self.__alluser[self.__user]['money']}\n按回车返回上级菜单")
                input()
                self.main_menu()
        else:
            print('请输入纯数字')
            self.check_out()

    def save_money(self):  # 存钱
        __save_money = input('请输入您想要存入的数目：')
        if __save_money.isnumeric():
            self.__alluser[self.__user]['money'] += int(__save_money)
            print(f"您现在所剩余额{self.__alluser[self.__user]['money']}\n按回车返回上级菜单")
            input()
            self.main_menu()
        else:
            print('请输入纯数字')
            self.save_money()

    def transfer(self):  # 转账
        __transfer_user = input('请输入收款账号姓名:')
        if __transfer_user in self.__alluser:
            __transfer_money = input(f'请问您打算给{__transfer_user}转账金额为：')
            if int( __transfer_money) > self.__alluser[self.__user]['money']:
                print('您的余额不够')
                self.transfer()
            else:
                self.__alluser[__transfer_user]['money'] += int( __transfer_money)
                print('你成功汇款')
                print(f"您现在所剩余额{self.__alluser[self.__user]['money']}\n按回车返回上级菜单")
                input()
                self.main_menu()
                print(self.__alluser[__transfer_user]['money'])
        else:
            print('很抱歉，未找到该用户')
            self.transfer()

    def exit(self):  # 离开
        self.login()


if __name__ == '__main__':
    abc = ATM()
    abc.login()
